﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MutFormStudio {
	public partial class StudioFormHead : UserControl {

		public StudioFormHead() {
			InitializeComponent();
		}
		public readonly Control control;
		public readonly TabStudio tag_studio;

		/// <summary>选中的顺序，如果用户关掉了最新的窗体，将会依据此变量选中上一个选中的窗体。 </summary>
		public int Selected_sequence { get; set; }	= 0;


		public StudioFormHead(Control c, TabStudio s) {
			InitializeComponent();
			mainBTN.Font = s.Font;
			control = c;
			tag_studio = s;
			if(c is StudioControl) {
				var sc = c as StudioControl;
				mainBTN.Text = sc.Head_text;
				BackColor = sc.Head_color;
				ContextMenuStrip = sc.HeadContextMenuStrip;
				sc.dHeadTestChanged = ()=> mainBTN.Text = sc.Head_text;
			} else {
				mainBTN.Text = c.Text;
			}
			mainBTN.DragEnter += (sender, e) => { tag_studio.headDragEnter(this, e); };
			mainBTN.DragDrop += (sender, e) => { tag_studio.headDragDrop(this, e); };
			mainBTN.MouseMove += (sender, e) => { tag_studio.headDragBegin(this, e); };
			mainBTN.Click += MainBTN_Click;
			s.FontChanged += S_FontChanged;

		}

		private void S_FontChanged(object sender, EventArgs e) {
			mainBTN.Font = tag_studio.Font;
		}

		internal void MainBTN_Click(object sender, EventArgs e) {
			tag_studio.showHeadControl(this);
		}
		bool is_selected = false;
		public bool Is_selected {
			get { return is_selected; }
			set {
				is_selected = value;
				if (is_selected) {
					control.Visible = true;
					mainBTN.BackColor = BackColor;
					CloseBTN.BackColor = BackColor;
				} else {
					control.Visible = false;
					mainBTN.BackColor = Color.White;
					CloseBTN.BackColor = Color.White;
				}
			}
		}
		private void StudioFormHead_BackColorChanged(object sender, EventArgs e) {
			if (is_selected) {
				control.Visible = true;
				mainBTN.BackColor = BackColor;
				CloseBTN.BackColor = BackColor;
			} else {
				control.Visible = false;
				mainBTN.BackColor = Color.White;
				CloseBTN.BackColor = Color.White;
			}
		}
		private void CloseBTN_Click(object sender, EventArgs e) {
			tag_studio.Base_studio.closeOneControl(this);
		}
		public override string ToString() {
			return mainBTN.Text;
		}

	}
}
